package sk.fhi.client;

import net.customware.gwt.presenter.client.Display;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.PlaceRequestEvent;
import net.customware.gwt.presenter.client.place.PlaceRequestHandler;
import sk.fhi.client.event.CancelEditEvent;
import sk.fhi.client.event.CancelEditEventHandler;
import sk.fhi.client.event.CreateEvent;
import sk.fhi.client.event.CreateEventHandler;
import sk.fhi.client.event.OpenEvent;
import sk.fhi.client.event.OpenEventHandler;
import sk.fhi.client.event.UpdateEvent;
import sk.fhi.client.event.UpdateEventHandler;
import sk.fhi.client.gin.CRUDGinjector;
import sk.fhi.client.presenter.AsbtractPresenter;
import sk.fhi.client.presenter.EditContactPresenter;
import sk.fhi.client.presenter.ListContactPresenter;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.inject.Inject;

public class AppController  implements AsbtractPresenter,  PlaceRequestHandler
{
	private EventBus eventBus;

	private HasWidgets container;

	private CRUDGinjector injector;
	
	private AsbtractPresenter presenter;

	@Inject
	public AppController(EventBus eventBus, CRUDGinjector injector)
	{
		this.eventBus = eventBus;
		this.injector = injector;
		bind();
	}

	public void bind()
	{
		eventBus.addHandler(PlaceRequestEvent.getType(), this);

		eventBus.addHandler(CreateEvent.TYPE, new CreateEventHandler()
		{
			@Override
			public void onCreate(CreateEvent event) {
				doAddNewContact();
			}

		});

		eventBus.addHandler(OpenEvent.TYPE, new OpenEventHandler()
		{

			@Override
			public void onOpen(OpenEvent event) {
				doEditContact(event.getEntity().getId());
			}
		});

		eventBus.addHandler(UpdateEvent.TYPE, new UpdateEventHandler()
		{
			@Override
			public void onUpdateEvent(UpdateEvent event) {
				doContactUpdated();
			}
		});

		eventBus.addHandler(CancelEditEvent.TYPE, new CancelEditEventHandler() {
			
			@Override
			public void onEditCancelled(CancelEditEvent event) {
				doEditContactCancelled();
			}
		});

	}

	private void doAddNewContact()
	{
		History.newItem(EditContactPresenter.PLACE.getId());
	}

	private void doEditContactCancelled()
	{
		History.newItem(ListContactPresenter.PLACE.getId());
	}

	protected void doContactUpdated()
	{
		History.newItem(ListContactPresenter.PLACE.getId());
	}

	private void doEditContact(Long contactId)
	{
		History.newItem(EditContactPresenter.PLACE.requestWith("contactId", contactId.toString()).toString());
	}

	public void go(final HasWidgets container)
	{

		this.container = container;

		if("".equals(History.getToken()))
		{
			History.newItem(ListContactPresenter.PLACE.getId());
		}
		else
		{
			History.fireCurrentHistoryState();
		}

	}

	@Override
	public void onPlaceRequest(PlaceRequestEvent event)
	{
		String id = event.getRequest().getPlace().getId();

		if(id.equals(ListContactPresenter.PLACE.getId()))
			presenter = injector.getListPresenter();
		else if(id.equals(EditContactPresenter.PLACE.getId()))
			presenter = injector.getEditContactPresenter();

		refreshDisplay();
	}

	@Override
	public void refreshDisplay()
	{
		container.clear();
		if(presenter != null)
		{
			presenter.go(container);
		}
	
	}

	@Override
	public Display getDisplay()
	{
		return presenter.getDisplay();
	}

	@Override
	public void revealDisplay()
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void unbind()
	{
		// TODO Auto-generated method stub

	}

}
